package com.pipi.fanglan.ui.controller;

import com.pipi.fanglan.cookie.CookieRepository;
import com.pipi.fanglan.model.MyCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.CookieStore;
import java.util.List;

/**
 * Created by pipi on 2017/7/17.
 */
@Controller
@RequestMapping("/monitor/user")
public class UserController {

    @Autowired
    private CookieRepository cookieRepository;

    @GetMapping("/list")
    public String userList(Model model){
        List<MyCookieStore> myCookieStoreList = cookieRepository.findAll();
        model.addAttribute("users",myCookieStoreList);
        return "/monitor/userlist";
    }

    @GetMapping("/remove")
    public String removeUser(String userName){
        cookieRepository.deleteCookieStore(userName);
        List<MyCookieStore> cookieStoreList = cookieRepository.findAll();
        if ( cookieStoreList!= null && cookieStoreList.size()>0){
            cookieStoreList.get(0).setActive(true);
        }
        return "redirect:/monitor/user/list";
    }

    @GetMapping("/active")
    public String setUserActive(String userName){
        MyCookieStore activeCookieStore = cookieRepository.findActive();
        if(activeCookieStore != null){
            activeCookieStore.setActive(false);
        }
        MyCookieStore myCookieStore = cookieRepository.findMyCookieStore(userName);
        if(myCookieStore != null){
            myCookieStore.setActive(true);
        }
        return "redirect:/monitor/list";
    }

}
